package com.scau.bookstore.service.impl;

import com.scau.bookstore.common.entity.Advertisement;
import com.scau.bookstore.contract.service.BookService;
import com.scau.bookstore.common.pojo.Book;
import com.scau.bookstore.common.pojo.Catalog;
import com.scau.bookstore.repository.mapper.BookDao;
import com.scau.bookstore.repository.mapper.CatalogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Autowired
    private CatalogDao catalogDao;

    /**
     * 显示所有分类及每个分类下前12条数据
     * @return
     */
    public List<Advertisement> findAdvertisements(){
        List<Advertisement> advertisementList = new ArrayList<>();
        List<Catalog> catalogList = catalogDao.findAll();
        for (Catalog catalog: catalogList){
            List<Book> bookList = bookDao.findTop12ByCatalogOrderByModifiedDateDesc(catalog);
            advertisementList.add(new Advertisement(catalog, bookList));
        }
        return advertisementList;
    }

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Book> findAll() {
        return bookDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Book> findSearch(Map whereMap, int page, int size) {
        Specification<Book> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return bookDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Book> findSearch(Map whereMap) {
        Specification<Book> specification = createSpecification(whereMap);
        return bookDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Book findById(Long id) {
        return bookDao.findById(id).get();
    }

    /**
     * 增加
     * @param book
     */
    public void add(Book book) {
        bookDao.save(book);
    }

    /**
     * 修改
     * @param book
     */
    public void update(Book book) {
        bookDao.save(book);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        bookDao.deleteById(id);
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Book> createSpecification(Map searchMap) {

        return (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // 书名
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                predicateList.add(cb.like(root.get("name").as(String.class), "%" + searchMap.get("name") + "%"));
            }
            // 副标题、宣传语
            if (searchMap.get("subtitle") != null && !"".equals(searchMap.get("subtitle"))) {
                predicateList.add(cb.like(root.get("subtitle").as(String.class), "%" + searchMap.get("subtitle") + "%"));
            }
            // 作者
            if (searchMap.get("author") != null && !"".equals(searchMap.get("author"))) {
                predicateList.add(cb.like(root.get("author").as(String.class), "%" + searchMap.get("author") + "%"));
            }
            // 出版社
            if (searchMap.get("publishingHouse") != null && !"".equals(searchMap.get("publishingHouse"))) {
                predicateList.add(cb.like(root.get("publishingHouse").as(String.class), "%" + searchMap.get("publishingHouse") + "%"));
            }
            // 分类
            if (searchMap.get("catalogId") != null && !"".equals(searchMap.get("catalogId"))) {
                predicateList.add(cb.equal(root.join("catalog").get("id"), searchMap.get("catalogId")));
            }
            // 状态
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                predicateList.add(cb.equal(root.get("status").as(Integer.class), searchMap.get("status")));
            }

            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

        };

    }

}
